<template>
  <div class="main">
    <div class="userTop">
      <div class="avatar">
        <img src="/static/images/index/logo.png" alt />
      </div>
    </div>
    <!-- 手机号 -->
    <div class="number">
      <img src="/static/images/index/telephone.png" alt />
      <input
        v-model="phone"
        type="number"
        maxlength="11"
        placeholder-style="color:#B8BAB9"
        placeholder="请输入手机号码"
      />
    </div>
    <!-- 验证码 -->
    <div class="code">
      <div class="codeL">
        <img src="/static/images/index/code.png" alt />
        <!-- maxlength="6" -->
        <input v-model="code" type="text" placeholder-style="color:#B8BAB9" placeholder="请输入短信验证码" />
      </div>
      <div class="codeR" @click="getCode">
        <span @click="getMsg">{{ getmsg }}</span>
      </div>
    </div>
    <!-- 提交按钮 -->
    <div class="btn" @click="login">
      <button>提交</button>
    </div>
    <img class="botB" src="/static/images/index/mountain.png" alt />
  </div>
</template>

<script>
import { sendSms, register, wechatToken } from '@/api/api';
// import { mapGetters } from 'vuex';
const reg = /^[1][2, 3,4,5, 6 ,7,8, 9][0-9]{9}$/;
export default {
  props: {},
  data () {
    return {
      getmsg: '获取验证码',
      changeMsg: true,
      phone: '',
      code: '',
      index: null
    };
  },
  // computed: {
  //   ...mapGetters(['userInfo'])
  // },
  created () {},
  mounted () {
    Object.assign(this.$data, this.$options.data());
    this.index = this.$mp.query.index ? this.$mp.query.index : 2;
    // console.log(userInfo, 'rrrrrr');
  },
  onShow () {
    this.index = this.$mp.query.index ? this.$mp.query.index : 2;
    if (this.$mp.query.code) {
      this.wechatToken();
    }
  },
  watch: {},
  methods: {
    // /** 公众号授权 */
    async wechatToken () {
      const code = this.$mp.query.code;
      await wechatToken({ code: code });
    },
    // 点击获取验证码判断手机号码
    // 获取短信验证码
    getCode () {
      if (!this.changeMsg) {
        return;
      }
      if (this.phone === '') {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 1000
        });
        return false;
      }
      if (!reg.test(this.phone)) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
          duration: 1000
        });
        return false;
      }
      sendSms({
        phone: this.phone
      })
        .then(res => {
          if (res.code === 200) {
            wx.showToast({
              title: '获取成功',
              icon: 'success',
              duration: 200
            });
            let time = 60;
            const timer = setInterval(() => {
              this.getmsg = time + 's';
              this.changeMsg = false;
              time--;
              if (time < 0) {
                clearInterval(timer);
                this.getmsg = '获取验证码';
                this.changeMsg = true;
              }
            }, 1000);
          }
        })
        .catch(error => {
          /** 捕获异常 */
          console.log(error, 'rrrrrrssssssssssssssss');
        });
    },
    // 点击提交判断手机号码和验证码，错误给提示，正确去首页
    login () {
      // eslint-disable-next-line no-undef
      let pages = getCurrentPages();
      let options = pages[pages.length - this.index].options;
      let url = '';
      let urlType = false;
      if (JSON.stringify(options) !== '{}') {
        urlType = true;
        // eslint-disable-next-line no-unused-vars
        for (let a in options) {
          url += `&${a}=${options[a]}`;
        }
        var newUrl = url.replace('&', '?');
      }
      console.log(newUrl, 'rrrrrrrrrr');
      let routeUrl =
        pages[pages.length - this.index].route + (urlType ? newUrl : '');
      // console.log(routeUrl, 'pagespagespages');
      if (this.phone === '') {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 1000
        });
        return;
      }
      if (!reg.test(this.phone)) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
          duration: 1000
        });
        return;
      }
      if (this.code === '') {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
      register({
        phone: this.phone,
        code: this.code
      }).then(res => {
        console.log(res);
        if (res === '') {
          this.$store.commit('userFun', routeUrl);
          // console.log(routeUrl, 'routeUrlrouteUrlrouteUrl');
          // wx.reLaunch({
          //   url: `/${routeUrl}`
          // });
        }
      });
      // wx.switchTab({ url: '/pages/index/index/main' });
    }
  },
  components: {}
};
</script>

<style scoped lang="less" scoped>
.main {
  display: flex;
  flex-direction: column;
  .userTop {
    margin-bottom: 86px;
    text-align: center;
    .avatar {
      width: 100%;
      margin-top: 66px;
      img {
        width: 169px;
        height: 68px;
      }
    }
  }
  // 手机号
  .number {
    border-bottom: 1px solid #e6e6e6ff;
    margin: 0 30px 25px;
    padding-bottom: 15px;
    display: flex;
    img {
      width: 20px;
      height: 20px;
      margin-right: 11px;
      vertical-align: middle;
    }
    input {
      width: 100%;
      height: 21px;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFangSC;
      font-weight: 400;
      color: #020604;
      line-height: 21px;
      vertical-align: middle;
    }
  }
  // 验证码
  .code {
    margin: 0 30px 41px;
    border-bottom: 1px solid #e6e6e6ff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 15px;
    .codeL {
      flex: 1;
      text-align: left;
      display: flex;
      img {
        width: 20px;
        height: 20px;
        margin-right: 11px;
      }
      input {
        width: 100%;
        height: 21px;
        font-size: 15px;
        font-family: PingFangSC-Regular, PingFangSC;
        font-weight: 400;
        color: #020604;
        line-height: 21px;
      }
    }
    .codeR {
      width: 72px;
      text-align: center;
      span {
        height: 20px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFangSC;
        font-weight: 400;
        color: rgba(24, 119, 152, 1);
        line-height: 20px;
      }
    }
  }
  // 提交按钮
  .btn {
    margin: 0 25px 205px;
    text-align: center;
    button {
      width: 100%;
      height: 46px;
      border: none;
      font-size: 15px;
      font-family: PingFangSC-Medium, PingFangSC;
      font-weight: 700;
      color: rgba(255, 255, 255, 1);
      background: rgba(24, 119, 152, 1);
      border-radius: 2px;
      line-height: 46px;
    }
  }
  // 背景图片
  .botB {
    width: 375px;
    height: 376px;
    position: absolute;
    bottom: 0;
    z-index: -1;
  }
}
</style>
